package com.crl.rcdp.sys.service;

import com.cars.util.bean.BasePage;
import com.cars.util.bean.ComboboxVO;
import com.cars.util.list.ListUtils;
import com.cars.util.string.StringUtil;
import com.crl.rcdp.sys.dao.SysRoleDao;
import com.crl.rcdp.sys.dao.SysRoleResourceDao;
import com.crl.rcdp.sys.dto.SysRoleDTO;
import com.crl.rcdp.sys.model.SysRole;
import com.crl.rcdp.sys.model.SysRoleResource;
import com.crl.rcdp.sys.vo.SysRoleSearchVO;
import com.crl.redis.RedisKey;
import com.crl.redis.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/21 17:05
 */
@Service
public class SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleResourceDao sysRoleResourceDao;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取所有角色
     */
    public BasePage<List<SysRoleDTO>> page(SysRoleSearchVO sysRoleSearchVO) {
        BasePage<List<SysRoleDTO>> basePage = new BasePage();
        int count = sysRoleDao.count(sysRoleSearchVO);
        basePage.setTotal(count);
        List<SysRole> list = sysRoleDao.list(sysRoleSearchVO);
        List<SysRoleDTO> listResult = ListUtils.copyList(list, SysRoleDTO.class);
        basePage.setData(listResult);
        return basePage;
    }

    /**
     * 新增
     *
     * @param sysRoleDTO
     * @return
     */
    public int add(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        int roleId = sysRoleDao.add(sysRole);
        String resourceIds = sysRoleDTO.getResourceIds();
        String[] resources = resourceIds.split("@");
        for (String resourceId : resources) {
            if (StringUtil.isNotNullOrEmpty(resourceId)) {
                SysRoleResource sysRoleResource = new SysRoleResource();
                sysRoleResource.setRoleId(roleId);
                sysRoleResource.setResourceId(Integer.parseInt(resourceId));
                sysRoleResourceDao.add(sysRoleResource);
            }
        }
        return 1;
    }

    /**
     * 修改
     *
     * @param sysRoleDTO
     * @return
     */
    public int update(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        sysRoleDao.update(sysRole);
        sysRoleResourceDao.deleteByRoleId(sysRole.getId());
        String resourceIds = sysRoleDTO.getResourceIds();
        String[] resources = resourceIds.split("@");
        for (String resourceId : resources) {
            if (StringUtil.isNotNullOrEmpty(resourceId)) {
                SysRoleResource sysRoleResource = new SysRoleResource();
                sysRoleResource.setRoleId(sysRole.getId());
                sysRoleResource.setResourceId(Integer.parseInt(resourceId));
                sysRoleResourceDao.add(sysRoleResource);
            }
        }
        //修改对应的redis的值
        redisUtil.del(RedisKey.AUTH_ROLE_PATH + sysRoleDTO.getId());
        redisUtil.set(RedisKey.AUTH_ROLE_PATH + sysRoleDTO.getId(), this.listRolePaths(sysRoleDTO.getId()));
        return 1;
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    public int delete(int roleId) {
        return sysRoleDao.delete(roleId);
    }

    /**
     * 获取单个角色
     *
     * @param id
     * @return
     */
    public SysRoleDTO get(int id) {
        SysRole sysRole = sysRoleDao.getById(id);
        SysRoleDTO sysRoleDTO = new SysRoleDTO();
        BeanUtils.copyProperties(sysRole, sysRoleDTO);
        return sysRoleDTO;
    }

    /**
     * 根据角色id获取角色对应的资源
     *
     * @param roleId
     * @return
     */
    public List<SysRoleResource> listRoleResources(int roleId) {
        return sysRoleResourceDao.listByRoleId(roleId);
    }


    /**
     * 角色列表下拉框
     *
     * @return
     */
    public List<ComboboxVO> listSysRoleCombo() {
        List<ComboboxVO> listRet = sysRoleDao.listCombo();
        return listRet;
    }


    /**
     * 根据roleId获取角色对应的资源url
     *
     * @param roleId
     * @return
     */
    public HashMap<String, String> listRolePaths(int roleId) {
        List<SysRoleResource> listRoleResource = sysRoleResourceDao.listByRoleId(roleId);
        HashMap<String, String> hashMap = new HashMap<>();
        for (SysRoleResource sysRoleResource : listRoleResource) {
            if (StringUtil.isNotNullOrEmpty(sysRoleResource.getPath())) {
                hashMap.put(sysRoleResource.getPath(), sysRoleResource.getPath());
            }
        }
        return hashMap;
    }

}
